﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class SpecificAdjustManageController : AdminBaseController
    {
        //
        // GET: /SpecificAdjustManage/
        [AuthorityActionFilter("m_SpecificAdjustManage", Order = 1)]
        [LogPageActionFilter("特殊调整", Order = 2)]
        public ActionResult Index()
        {
            List<Model.SpecificAdjust> SpecificAdjustList = new List<Model.SpecificAdjust>();
            SpecificAdjustList = GetSpecificAdjustList(" FAllowanceFileEntryID is null and sFStatus<> '2' ", 1);
            foreach (Model.SpecificAdjust sj in SpecificAdjustList)
            {
                if (sj!=null)
                {
                    sj.FAdjustAmount = sj.FAdjustAmount / 10000;
                }
                
            }
            return View(SpecificAdjustList);
        }

        public ActionResult Save(Model.SpecificAdjust sAm)
        {
            try
            {
                BLL.SpecificAdjust sBLL = new BLL.SpecificAdjust();
                BLL.AllowanceIssue aiBLL = new BLL.AllowanceIssue();
                Model.AllowanceIssue a = aiBLL.GetModel(sAm.FIssueID);
                if (a != null)
                {
                    sAm.FIssueID = a.FIssueID;
                    switch (Fetch.Post("hidType"))
                    {
                        case "ADD":
                            sAm.FEntityNo = GlobalConstant.ENTITYNO;
                            sAm.FInsDate = DateTime.Now;
                            sAm.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                            sAm.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                            sAm.FInsIP = Common.GenerateUtil.GetClientIPv4Address();
                            sAm.FUpdDate = DateTime.Now;
                            sAm.FUpdDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                            sAm.FUpdEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                            sAm.FUpdIP = Common.GenerateUtil.GetClientIPv4Address();
                            sAm.FAdjustAmount = sAm.FAdjustAmount * 10000;
                            sAm.FStatus = "1";
                            sBLL.Add(sAm);
                            break;
                        case "EDIT":
                            sAm.FUpdDate = DateTime.Now;
                            sAm.FUpdDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                            sAm.FUpdEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                            sAm.FUpdIP = Common.GenerateUtil.GetClientIPv4Address();
                            sAm.FAdjustAmount = sAm.FAdjustAmount * 10000;
                            sAm.FStatus = "1";
                            sBLL.Update(sAm);
                            break;
                    }

                    ViewData["jsAlert"] = "操作成功";
                    ViewData["type"] = Fetch.Post("hidType");
                }
                else
                {

                    ViewData["jsAlert"] = "请填写正确的期号！";
                    ViewData["type"] = Fetch.Post("hidType");
                }

            }
            catch (Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
                ViewData["type"] = Fetch.Post("hidType");
            }
            BLL.SpecialAdjustCls sPCBLL = new BLL.SpecialAdjustCls();
            ViewData["SpecialAdjustClsList"] = sPCBLL.GetModelList(" FYear=" + Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            Model.DJ_NSRXX d = DJ_NSRXXBLL.GetModel(sAm.NSRDZDAH, Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            sAm.NSRBM = d.NSRBM;
            sAm.FYear = decimal.Parse(Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            sAm.NSRMC = d.NSRMC;
            sAm.FEnterprisePropNo = d.FEnterprisePropNo;
            sAm.FEnterprisePropName = d.FEnterprisePropName;
            sAm.FFinancePropNo = d.FFinancePropNo;
            sAm.FFinancePropName = d.FFinancePropName;
            ViewData["FSpecialAdjustClsNo"] = sAm.FSpecialAdjustClsNo;
            if (ViewData["jsAlert"].ToString() == "操作成功"&ViewData["type"].ToString() == "ADD")
            {
                return SpecificAdjustEdit("", sAm.NSRBM);
            }
            return View("SpecificAdjustEdit", sAm);
        }

        public string DelSpecificAdjust(string specificAdjustNo)
        {
            string str = string.Empty;
            if (null != specificAdjustNo)
            {
                specificAdjustNo = Fetch.estimateString(specificAdjustNo, CheckGetEnum.Safety);
                BLL.SpecificAdjust codeNameBll = new BLL.SpecificAdjust();
                try
                {
                    codeNameBll.Delete(specificAdjustNo);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }

        public ActionResult SpecificAdjustEdit(string specificAdjustNo, string nsrbm)
        {
            BLL.SpecificAdjust sPBLL = new BLL.SpecificAdjust();
            Model.SpecificAdjust sAm = new Model.SpecificAdjust(); 
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            BLL.SpecialAdjustCls sPCBLL = new BLL.SpecialAdjustCls();
            ViewData["SpecialAdjustClsList"] = sPCBLL.GetModelList(" FYear=" + Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            ViewData["type"] = "NO";
            BLL.AllowanceIssue ai = new BLL.AllowanceIssue();
            ViewData["IssueList"] = ai.GetModelList(" FStatus <> '2'");
            if (specificAdjustNo != null && specificAdjustNo != "")
            {
                sAm = sPBLL.GetModel(specificAdjustNo);
                Model.DJ_NSRXX d = DJ_NSRXXBLL.GetModel(sAm.NSRDZDAH, Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                sAm.NSRBM = d.NSRBM;
                sAm.FYear = decimal.Parse(Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                sAm.NSRMC = d.NSRMC;
                sAm.FEnterprisePropNo = d.FEnterprisePropNo;
                sAm.FEnterprisePropName = d.FEnterprisePropName;
                sAm.FFinancePropNo = d.FFinancePropNo;
                sAm.FFinancePropName = d.FFinancePropName;
              //  sAm.FAdjustAmount = 0;
                     //BLL.AllowanceIssue ai = new BLL.AllowanceIssue();
                //Model.AllowanceIssue aiM = ai.GetModel(sAm.FIssueID);
                //sAm.FIssueNo = aiM.FIssueNo;
                ViewData["type"] = "EDIT";
            }
            else
            {
                if (nsrbm != null && nsrbm != "")
                {
                    Model.DJ_NSRXX d = DJ_NSRXXBLL.GetModelByNSRBMlike(nsrbm, Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                    if (d != null)
                    {
                        sAm.NSRBM = d.NSRBM;
                        sAm.NSRDZDAH = d.NSRDZDAH;
                        sAm.FYear = decimal.Parse(Common.OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                        sAm.NSRMC = d.NSRMC;
                        sAm.FEnterprisePropNo = d.FEnterprisePropNo;
                        sAm.FEnterprisePropName = d.FEnterprisePropName;
                        sAm.FFinancePropNo = d.FFinancePropNo;
                        sAm.FFinancePropName = d.FFinancePropName;
                        sAm.FAdjustAmount = 0;
                        sAm.FNote = "";
                        ViewData["type"] = "ADD";
                        if (  ViewData["FSpecialAdjustClsNo"] !=null )
                        {
                            sAm.FSpecialAdjustClsNo = ViewData["FSpecialAdjustClsNo"].ToString();
                        }
                         
                        Model.AllowanceIssue aiM = ai.GetLastModel();
                        if (aiM != null)
                        {
                            sAm.FIssueID = aiM.FIssueID;
                            sAm.FIssueNo = aiM.FIssueNo;
                            sAm.FYear = decimal.Parse(aiM.FYear.ToString());
                        }
                    }
                }
            }


            return View("SpecificAdjustEdit", sAm);
        }

        /// <summary>
        /// 构造显示DT的总和行
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public DataTable GetFormerYearSpecificAdjustList(DataTable dt)
        {
            dt.Columns.Add("xh");
            int i = 0;
            foreach (DataRow drz in dt.Rows)
            {
                i++;
                drz["xh"] = i;
                if (drz["FAdjustAmount"] == null && drz["FAdjustAmount"].ToString() == "")
                {
                    drz["FAdjustAmount"] = 0;
                }
                else
                {
                    drz["FAdjustAmount"] = (decimal.Parse(drz["FAdjustAmount"].ToString()) / 10000);
                }

            }

            dt.Columns["FAdjustAmount"].DataType = typeof(decimal);
            decimal sumse = dt.AsEnumerable().Sum(p => p.Field<decimal>("FAdjustAmount"));

            DataRow dr = dt.NewRow();
            dr["xh"] = "合计(万元)";
            dr["FAdjustAmount"] = sumse;
            dt.Rows.Add(dr);
            return dt;
        }

        public ActionResult SpecificAdjustListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string NSRBM = Request.Params["NSRBM"];
            string NSRMC = Request.Params["NSRMC"];
            string FIssueNo = Request.Params["FIssueNo"];
            
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "  FAllowanceFileEntryID is null and sFStatus<> '2' ";

            if (NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
            }

            if (NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
            }

            if (FIssueNo != "")
            {
                strwhere += " and FIssueNo like '%" + FIssueNo + "%'";
            }
            
            List<Model.SpecificAdjust> SpecificAdjustList = new List<Model.SpecificAdjust>();
            SpecificAdjustList = GetSpecificAdjustList(strwhere, pageIndex);
            foreach (Model.SpecificAdjust sj in SpecificAdjustList)
            {
                if (sj!=null)
                {
                    sj.FAdjustAmount = sj.FAdjustAmount / 10000;
                }
                
            }
            return PartialView("SpecificAdjustListPartial", SpecificAdjustList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.SpecificAdjust> GetSpecificAdjustList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.SpecificAdjust SpecificAdjustBLL = new BLL.SpecificAdjust();
            string fiedls = "FSpecificAdjustNo";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.v_SpecificAdjust", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FSpecificAdjustNo", out total);
            List<Model.SpecificAdjust> SpecificAdjustList = SpecificAdjustBLL.DataTableToList(ds.Tables[0]);
            List<Model.SpecificAdjust> SpecificAdjustAllList = new List<Model.SpecificAdjust>();
            total = SpecificAdjustBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                SpecificAdjustAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (SpecificAdjustList.Count > i)
                {
                    SpecificAdjustAllList[((pageIndex - 1) * pageRowSize) + i] = SpecificAdjustList[i];
                }
            }
            return SpecificAdjustAllList;
        }

        public ActionResult FormerYearSpecificAdjustListPartial()
        {
            return View();
        }

        public ActionResult FormerYearSpecificAdjustShow(string NSRDZDAH)
        {
            BLL.SpecificAdjust sPBLL = new BLL.SpecificAdjust();
            DataTable sL = new DataTable();
            if (NSRDZDAH == null)
            {
                NSRDZDAH = "-1";
            }
            sL = sPBLL.GetList(" a.NSRDZDAH ='" + NSRDZDAH + "'").Tables[0];
            sL = GetFormerYearSpecificAdjustList(sL);
            return View(sL);
        }

    }
}
